"use strict";

var EXPORTED_SYMBOLS = ["btn2code"]

Components.utils.import("resource://gre/modules/Services.jsm")

// The preference that tells the number of rows of the newtab grid.
const PREF_NEWTAB_ROWS = "browser.newtabpage.rows";

// The preference that tells the number of columns of the newtab grid.
const PREF_NEWTAB_COLUMNS = "browser.newtabpage.columns";

var btn2code = {

    __dateService: null,
    get _dateService()
    {
        if(!(this.__dateService))
            this.__dateService = Components.classes["@mozilla.org/intl/scriptabledateformat;1"].
                           getService(Components.interfaces.nsIScriptableDateFormat);
        return this.__dateService;
    },
//  date SSystem from: chrome://browser/content/places/treeView.js
    convertPRTimeToString: function (aTime)
    {
        const MS_PER_MINUTE = 60000;
        const MS_PER_DAY = 86400000;
        let timeMs = aTime / 1000; // PRTime is in microseconds
    
// Date is calculated starting from midnight, so the modulo with a day are milliseconds from today's midnight.
// getTimezoneOffset corrects that based on local time, notice midnight can have a different offset during DST-change days.
        let dateObj = new Date();
        let now = dateObj.getTime() - dateObj.getTimezoneOffset() * MS_PER_MINUTE;
        let midnight = now - (now % MS_PER_DAY);
        midnight += new Date(midnight).getTimezoneOffset() * MS_PER_MINUTE;
    
        let dateFormat = timeMs >= midnight ?
                Components.interfaces.nsIScriptableDateFormat.dateFormatNone :
                Components.interfaces.nsIScriptableDateFormat.dateFormatShort;
    
        let timeFormat = Components.interfaces.nsIScriptableDateFormat.timeFormatNoSeconds;
        if(dateFormat) timeFormat =  Components.interfaces.nsIScriptableDateFormat.timeFormatNone;
        
        let timeObj = new Date(timeMs);
        return (this._dateService.FormatDateTime("", dateFormat, timeFormat,
            timeObj.getFullYear(), timeObj.getMonth() + 1,
            timeObj.getDate(), timeObj.getHours(),
            timeObj.getMinutes(), timeObj.getSeconds()));
    },

//  addEventListener.
//  in:     "new tab" document,   browser handler
//  out:    append handler to "wind rose" of document

//  protocol.
//  isEnabled:  in rows,
//          return: enabled status.
//  setPref:    in rows,
//          out:    put new value to preference
//          return: less or more.
//      less is casus belli to clearing of gPinnedLinks.
    rowMinus : {
        pref : PREF_NEWTAB_ROWS,

        isEnabled : function(aGridPrefs) {
            return (aGridPrefs.gridRows >> 2);
        },

        setPref : function(aGridPrefs) {
            let theval = aGridPrefs.gridRows - 1;
            if(theval < 3) theval = 3;
            Services.prefs.setIntPref(this.pref, theval);
            return (theval < aGridPrefs.gridRows);
        }
    },

    rowPlus : {
        pref : PREF_NEWTAB_ROWS,

        isEnabled : function(aGridPrefs) {
            return (aGridPrefs.gridRows < 8);
        },

        setPref : function(aGridPrefs) {
            let theval = aGridPrefs.gridRows + 1;
            if(theval >> 3) theval = 8;
            Services.prefs.setIntPref(this.pref, theval);
            return (theval > aGridPrefs.gridRows);
        }
    },

    colMinus: {
        pref : PREF_NEWTAB_COLUMNS,

        isEnabled : function(aGridPrefs) {
            return (aGridPrefs.gridColumns >> 2);
        },

        setPref : function(aGridPrefs) {
            let theval = aGridPrefs.gridColumns - 1;
            if(theval < 3) theval = 3;
            Services.prefs.setIntPref(this.pref, theval);
            return (theval < aGridPrefs.gridColumns);
        }
    },

    colPlus: { 
        pref : PREF_NEWTAB_COLUMNS,

        isEnabled : function(aGridPrefs) {
            return (aGridPrefs.gridColumns < 8);
        },

        setPref : function(aGridPrefs) {
            let theval = aGridPrefs.gridColumns + 1;
            if(theval >> 3) theval = 8;
            Services.prefs.setIntPref(this.pref, theval);
            return (theval > aGridPrefs.gridColumns);
        }
    },    
}
